//引入模块
const http = require("http");
const url = require("url");
const path = require("path");
const fs = require("fs");
const mimeModule = require("./module/mimeModule");

http.createServer(function (request, response) {

    let pathname = url.parse(request.url).pathname;
    // 获取文件后缀名
    let extname = path.extname(pathname);

    if (pathname === "/") pathname = "index.html";

    if (pathname !== "/favicon.ico") {
        let head = {};
        head.code = 200;
        head.type = "text/html";
        head.charset = "UTF-8";
        fs.readFile("./static/" + pathname, function (err, data) {
            if (!err) {
                // head.type = mimeModule.getType(extname);
                head.type = mimeModule.getMime(fs, extname);
                mimeModule.resOut(response, head, data);
            } else {
                head.code = 404;
                fs.readFile("./static/404.html", function (err, data404) {
                    mimeModule.resOut(response, head, data404);
                });
            }
        });
    }
}).listen(8080);

console.log("nodejs 8080 port is starting...");